package com.exmaple.lemlin.mvp.presenter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.app.Constants;
import com.exmaple.lemlin.entity.AccessEvenBus;
import com.exmaple.lemlin.mvp.base.BasePresenter;
import com.exmaple.lemlin.mvp.view.IAccessView;
import com.exmaple.lemlin.rxjava.RxSubscribe;
import com.exmaple.lemlin.utils.ACache;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.xmpp.XmppConnection;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Administrator
 */
public class AccessPresenter extends BasePresenter<IAccessView> {

    public AccessPresenter(Context context, IAccessView view) {
        super(context, view);
    }

    /**
     * 用户登录
     *
     * @param bus 用户名密码
     */
    public void userLogin(@NonNull final AccessEvenBus.Authenticate bus) {

        if (TextUtils.isEmpty(bus.getUserName())) {
            mView.onHint("请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(bus.getUserPwd())) {
            mView.onHint("请输入密码");
            return;
        }

        Observable<Boolean> booleanObservable = Observable.just(bus).subscribeOn(Schedulers.io())
                .map(new Function<AccessEvenBus.Authenticate, Boolean>() {
                    @Override
                    public Boolean apply(AccessEvenBus.Authenticate authenticate) throws Exception {
                        return XmppConnection.getInstance()
                                .userLogin(authenticate.getUserName(), authenticate.getUserPwd());
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        booleanObservable.subscribe(new RxSubscribe<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mView.onSuccessLogin();
                } else {
                    mView.onHint("登录失败！");
                }
            }

            @Override
            public void onHint(String hint) {
                mView.onHint("登录失败！" + hint);
                XmppConnection.getInstance().closeConnection1();
            }
        });
    }

    /**
     * 用户注册
     *
     * @param bus 用户名密码确认密码
     */
    public void userRegister(@NonNull AccessEvenBus.Authenticate bus) {


        if (TextUtils.isEmpty(bus.getUserName())) {
            mView.onHint("请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(bus.getUserPwd())) {
            mView.onHint("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(bus.getUserPwdSure()) && bus.getUserName().equals(bus.getUserPwdSure())) {
            mView.onHint("确认密码错误");
            return;
        }

        Observable<Boolean> booleanObservable = Observable.just(bus).subscribeOn(Schedulers.io())
                .map(new Function<AccessEvenBus.Authenticate, Boolean>() {
                    @Override
                    public Boolean apply(AccessEvenBus.Authenticate authenticate) throws Exception {
                        return XmppConnection.getInstance()
                                .userLogon(authenticate.getUserName(), authenticate.getUserPwd());
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        booleanObservable.subscribe(new RxSubscribe<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mView.onSuccessRegister();
                } else {
                    mView.onHint("注册失败！");
                }
            }

            @Override
            public void onHint(String hint) {
                mView.onHint("注册失败！" + hint);

            }
        });
    }


    public boolean isAuthenticated() {
        return XmppConnection.getInstance().isAuthenticated();
    }

}
